'use strict';

import React, {
    Component
} from 'react';

import {
    StyleSheet,
    View,
    AsyncStorage,
    Platform,
    Text,
    BackAndroid,
    NativeModules,
} from 'react-native';
import {connect} from 'react-redux';
import Splash from '../Containers/Splash';
import Storage from 'react-native-storage';
// import JPushModule from 'jpush-react-native';
import Global from '../Commom/GlobalConst';
var {NativeAppEventEmitter} = require('react-native');
import { Navigator } from 'react-native-deprecated-custom-components'
// import MainViewContainers from './MainViewContainers';
//-----------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------
var storage = new Storage({
    // 最大容量，默认值1000条数据循环存储
    size: 1000,
    // 存储引擎：对于RN使用AsyncStorage，对于web使用window.localStorage
    // 如果不指定则数据只会保存在内存中，重启后即丢失
    storageBackend: AsyncStorage,
    // 数据过期时间，默认一整天（1000 * 3600 * 24 毫秒），设为null则永不过期
    defaultExpires: null,
    // 读写时在内存中缓存数据。默认启用。
    enableCache: true,
    // 如果storage中没有相应数据，或数据已过期，
    // 则会调用相应的sync同步方法，无缝返回最新数据。
    sync: {}
});
global.storage = storage;
//-----------------------------------------------------------------------------------
export default class App extends Component {
    componentDidMount() {
        storage.load({
            key: Global.Const.storageToken,
        }).then((ret) => {
            Global.Const.token = ret.token;
        }).catch((err) => {

        });
        if (Platform.OS === 'ios') {
            // var subscriReceive = NativeAppEventEmitter.addListener(
            //     'ReceiveNotification',
            //     (notification) => console.log(notification)
            // );
            // var subscriOpen = NativeAppEventEmitter.addListener(
            //     'OpenNotification',
            //     (notification) => console.log(notification)
            // );
            //
            // // NativeModules.JPushModule.getRegistrationID((registrationId) => {
            //     Global.Const.registeredId = registrationId;
            // // });


        } else {
            BackAndroid.addEventListener('hardwareBackPress', this.onBackAndroid);
            // JPushModule.initPush();
            // JPushModule.getRegistrationID((registrationId) => {
            //     Global.Const.registeredId = registrationId;
            // });
            // JPushModule.addReceiveCustomMsgListener((message) => {
            //     console.log(message);
            // });
            // JPushModule.addReceiveNotificationListener((map) => {
            //     console.log("alertContent: " + map.alertContent);
            //     console.log("extras: " + map.extras);
            //     var extra = JSON.parse(map.extras);
            //     console.log(extra.key + ": " + extra.value);
            // });
            // JPushModule.addReceiveOpenNotificationListener((map) => {
            //     console.log("Opening notification!");
            //     console.log(map);
            //     console.log(map['cn.jpush.android.ALERT']);
            // });
        }
    }

    componentWillUnmount() {

        if (Platform.OS === 'ios') {
            NativeAppEventEmitter.removeAllListeners();
        } else {
            // JPushModule.removeReceiveCustomMsgListener();
            // JPushModule.removeReceiveNotificationListener();
            // JPushModule.removeReceiveOpenNotificationListener();
            BackAndroid.removeEventListener('hardwareBackPress', this.onBackAndroid)
        }
        // DeviceEventEmitter.removeAllListeners();

    }

    onBackAndroid = ()=> {
        const nav = this.refs['navigator'];
        if (nav === undefined) return false;
        const routers = nav.getCurrentRoutes();
        if (routers.length > 1) {
            nav.pop();
            return true;
        }
        if (Platform.OS === 'android') {
            NativeModules.ExitAPPModule.exitApp();
        }
        return false;
    };

    render() {
        return (
            <View style={{ flex: 1 }}>
                <Navigator ref="navigator"
                           initialRoute={{ name: 'Splash', component: Splash }}
                           configureScene={(route) => {
                if (route.sceneConfig) {
                    return route.sceneConfig;
                }
                return Navigator.SceneConfigs.FloatFromRight;
            } }
                           renderScene={(route, navigator) => {
                let Component = route.component;
                return (
                    <Component navigator = {navigator} route = {route} {...route.passProps} />
                );
            }}/>
            </View>
        );
    }
}
